/*
 * LzStubs.java July 2009
 *
 * Copyright (C) 2009, Quirino Zagarese <quirino@laszloitalia.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA  02111-1307  USA
 */
package org.eu4ria.tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.Date;
import java.util.Enumeration;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

import javax.swing.JFrame;

import org.eu4ria.Application;
import org.eu4ria.core.AppRepository;
import org.eu4ria.core.ArchiveDeployer;
import org.eu4ria.tools.lzstubs.LZXStub;
import org.eu4ria.tools.lzstubs.LZXStubTest;
import org.eu4ria.tools.lzstubs.LZXStubsFrame;

public class LZStubs {

	private static Properties props = new Properties();


	private static JarFile thisJar;

	public static void main(String[] args) throws Exception {

		thisJar = new JarFile(new File(LZStubs.class
				.getProtectionDomain().getCodeSource().getLocation().toURI()));

		Enumeration<JarEntry> entries = thisJar.entries();
		while (entries.hasMoreElements()) {
			JarEntry entry = entries.nextElement();
			System.out.println(entry.getName());
		}

		LZXStubsFrame frame = LZXStubsFrame.getInstance();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.pack();
		loadProperties();
		frame.setVisible(true);
	}

	public static void loadProperties() throws IOException,
			FileNotFoundException {
		LZXStubsFrame frame = LZXStubsFrame.getInstance();
		File f = new File("props.xml");
		if (!f.exists()) {
			props.setProperty("input", "");
			props.setProperty("output", "");
			props.setProperty("host", "localhost");
			props.setProperty("port", "8080");
			props.setProperty("timeout", "30000");
			props.setProperty("stubtest", "true");
			props.storeToXML(new FileOutputStream(f), "default params");
		}
		try {
			props.loadFromXML(new FileInputStream(new File("props.xml")));
			frame.getJarPathField().setText(props.getProperty("input"));
			frame.getOutputPathField().setText(props.getProperty("output"));
			frame.getHostField().setText(props.getProperty("host"));
			frame.getPortField().setText(props.getProperty("port"));
			frame.getTimeoutField().setText(props.getProperty("timeout"));
			frame.getStubTestCheckBox().setSelected(
					Boolean.parseBoolean(props.getProperty("stubtest")));
		} catch (InvalidPropertiesFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void createStubs(File input, File outputFolder, String host,
			int port, int timeout, boolean createStubTest) throws Exception {
		if (!(input.exists() && (input.getAbsolutePath().endsWith(".jar")))) {
			throw new Exception("Input file is not valid or does not exist.");
		}
		if (!host.startsWith("http://")) {
			host = "http://" + host;
		}

		ArchiveDeployer deployer = ArchiveDeployer.getInstance();
		deployer.deployApplication(input, false);
		Application application = AppRepository.getInstance().getApplication(
				input.getName());

		LZXStub stub = new LZXStub(application, thisJar, host + ":" + port
				+ "/EU4RIA/eu4ria", timeout + "");

		try {
			PrintStream stubPrintStream = new PrintStream(new FileOutputStream(new File(
					outputFolder.getAbsolutePath() + File.separator
							+ "stubs.lzx")));
			stubPrintStream.print(stub.serialize());
			if (createStubTest) {
				PrintStream stubtestPrintStream = new PrintStream(new FileOutputStream(new File(
						outputFolder.getAbsolutePath() + File.separator
								+ "stubtest.lzx")));
				stubtestPrintStream.print(new LZXStubTest(application,LZStubs.thisJar).serialize());
			}
		} catch (Exception e) {
			throw new Exception(
					"Impossible to write stubs to disk. Please check filesystem permissions.");
		}
	}

	public static void saveProperties() {
		LZXStubsFrame frame = LZXStubsFrame.getInstance();
		File f = new File("props.xml");
		props.setProperty("input", frame.getJarPathField().getText());
		props.setProperty("output", frame.getOutputPathField().getText());
		props.setProperty("host", frame.getHostField().getText());
		props.setProperty("port", frame.getPortField().getText());
		props.setProperty("timeout", frame.getTimeoutField().getText());
		props.setProperty("stubtest", frame.getStubTestCheckBox().isSelected()
				+ "");
		try {
			props.storeToXML(new FileOutputStream(f), "Properties saved on "
					+ new Date(System.currentTimeMillis()));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
